package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSeneitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSeneitiveService {

    @Autowired
    AdSensitiveMapper adSensitiveMapper;
    @Override
    public ResponseResult<AdSensitive> findAllSensitive(SensitiveDTO sensitiveDTO) {
        //检查参数
        sensitiveDTO.checkParam();
        //根据名称进行模糊分页查询
        IPage page = new Page(sensitiveDTO.getPage(), sensitiveDTO.getSize());
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sensitiveDTO.getName())){
           lambdaQueryWrapper.like(AdSensitive::getSensitives,sensitiveDTO.getName());
        }
        IPage iPage = this.page(page, lambdaQueryWrapper);
        //结果解析
        return new PageResponseResult(sensitiveDTO.getPage(), sensitiveDTO.getSize(), iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public ResponseResult<AdSensitive> insertSensitive(AdSensitive adSensitive) {
        //参数检验
        if (adSensitive == null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"参数为空");
        }
        if (!StringUtils.isNotBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数无效");
        }
        //判断参数存在不
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在");

        }
        //添加敏感词
        adSensitive.setCreatedTime(new Date());
        boolean save = this.save(adSensitive);
        if (save){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"敏感词添加失败");
    }

    @Override
    public ResponseResult<AdSensitive> updateSensitive(AdSensitive adSensitive) {
        //参数校验
         //参数校验
        if (adSensitive == null || adSensitive.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"无参数，或是没有参数id");
        }
        //判断该频道是否存在，存在名称是否唯一
        AdSensitive sensitive = this.getById(adSensitive.getId());
        if (sensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"敏感词不存在");

        }
        if (StringUtils.isNotBlank(adSensitive.getSensitives()) && !sensitive.getSensitives().equals(adSensitive.getSensitives())){
            //判断新的频道名称在数据库是否存在
            int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
            if (count>0){//频道名称在数据库已存在
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词已存在");

            }
        }
        //名称存在，修改
        boolean b = this.updateById(adSensitive);
        if (b){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR,"修改敏感词失败");
    }

    @Override
    public ResponseResult<AdSensitive> deleteSensitive(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"敏感词为空");
        }
        //判断该敏感词存不存在
        AdSensitive adSensitive = this.getById(id);
        if (adSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST,"敏感词不存在");

        }
        boolean b = this.removeById(id);
        if (b){
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"敏感词删除失败！");
    }

    /*
    * 一次性查询所有敏感词
    * */
    @Override
    public ResponseResult<List<String>> selectAllSensitive() {
        List<String> list = adSensitiveMapper.selectSensitiveAll();
        if (list == null || list.size()<=0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"无敏感词");
        }
        return ResponseResult.okResult(list);
    }
}
